/*
*  Copyright (c) 2006-2020, Chukie
*
*  SPDX-License-Identifier: Apache-2.0
*
*  @file     : cellular_hal.c
*
*  @brief    :
*  Website   : https://gitee.com/open-embedded
*
*
*  Change Logs:
*  Date           Author          Notes
*  2020-06-23     zhaocj       The first version
*/
/* Includes -----------------------------------------------------------------*/
#include "cellular_def.h"
/* Private typedef ----------------------------------------------------------*/
/* Private define -----------------------------------------------------------*/
/* Private variables --------------------------------------------------------*/
/* Extern variables ---------------------------------------------------------*/
/* Private function prototypes ----------------------------------------------*/


/* Public function prototypes -----------------------------------------------*/
/**
 * @brief hardware init
 *
 * @return
 */
RT_WEAK void cellular_hw_init(void)
{
#ifdef CELLULAR_USING_DUAL_SIM
    if (strlen(CELLULAR_SIM_SWITCH_PIN_NAME))
        rt_pin_mode(rt_pin_get(CELLULAR_SIM_SWITCH_PIN_NAME), PIN_MODE_OUTPUT);
#endif /*CELLULAR_USING_DUAL_SIM*/

#ifdef CELLULAR_USING_MODULE_POWER_DRIVER
    if (strlen(MODULE_POWER_CTRL_PIN_NAME))
    {
        rt_pin_mode(rt_pin_get(MODULE_POWER_CTRL_PIN_NAME), PIN_MODE_OUTPUT);
        rt_pin_write(rt_pin_get(MODULE_POWER_CTRL_PIN_NAME), !MODULE_POWER_ON_PIN_LEVEL);
    }

    if (strlen(MODULE_POWER_KEY_PIN_NAME))
        rt_pin_mode(rt_pin_get(MODULE_POWER_KEY_PIN_NAME), PIN_MODE_OUTPUT);
    if (strlen(MODULE_RESET_PIN_NAME))
        rt_pin_mode(rt_pin_get(MODULE_RESET_PIN_NAME), PIN_MODE_OUTPUT);
#endif /*CELLULAR_USING_MODULE_POWER_DRIVER*/

#ifdef CELLULAR_USING_MODULE_SLEEP_CTRL_DRIVER
    if (strlen(MODULE_SLEEP_STATE_PIN_NAME))
        rt_pin_mode(rt_pin_get(MODULE_SLEEP_STATE_PIN_NAME), PIN_MODE_INPUT);
    if (strlen(MODULE_SLEEP_CTRL_PIN_NAME))
        rt_pin_mode(rt_pin_get(MODULE_SLEEP_CTRL_PIN_NAME), PIN_MODE_OUTPUT);
#endif /*CELLULAR_USING_MODULE_SLEEP_CTRL_DRIVER*/
}

/**
 * @brief switch sim
 *
 * @param idx sim index,allowed value: 0~1
 * @return
 */
RT_WEAK void cellular_hw_sim_switch(uint8_t idx)
{
#ifdef CELLULAR_USING_DUAL_SIM
    if (!strlen(CELLULAR_SIM_SWITCH_PIN_NAME))
    {
        return;
    }
    if (0 == idx)
    {
        rt_pin_write(rt_pin_get(CELLULAR_SIM_SWITCH_PIN_NAME), !CELLULAR_SIM_SWITCH_SIM2_PIN_LEVEL);
    }
    else
    {
        rt_pin_write(rt_pin_get(CELLULAR_SIM_SWITCH_PIN_NAME), CELLULAR_SIM_SWITCH_SIM2_PIN_LEVEL);
    }
#endif /*CELLULAR_USING_DUAL_SIM*/
}

/**
 * @brief Set cellular power
 *
 * @param on    0: power-off; 1: power on
 * @return
 */
RT_WEAK void cellular_hw_set_power(int on)
{
#ifdef CELLULAR_USING_MODULE_POWER_DRIVER
    if (!strlen(MODULE_POWER_CTRL_PIN_NAME))
    {
        return;
    }
    if (on)
    {
        rt_pin_write(rt_pin_get(MODULE_POWER_CTRL_PIN_NAME), MODULE_POWER_ON_PIN_LEVEL);
    }
    else
    {
        rt_pin_write(rt_pin_get(MODULE_POWER_CTRL_PIN_NAME), !MODULE_POWER_ON_PIN_LEVEL);
    }
#endif /*CELLULAR_USING_MODULE_POWER_DRIVER*/
}

/**
 * @brief Set cellular power key level
 *
 * @param high 0: low level; 1: high level
 * @return
 */
RT_WEAK void cellular_hw_set_power_key(int high)
{
#ifdef CELLULAR_USING_MODULE_POWER_DRIVER
    if (!strlen(MODULE_POWER_KEY_PIN_NAME))
    {
        return;
    }
    if (high)
    {
        rt_pin_write(rt_pin_get(MODULE_POWER_KEY_PIN_NAME), MODULE_POWER_KEY_PIN_HIGH_LEVEL);
    }
    else
    {
        rt_pin_write(rt_pin_get(MODULE_POWER_KEY_PIN_NAME), !MODULE_POWER_KEY_PIN_HIGH_LEVEL);
    }
#endif /*CELLULAR_USING_MODULE_POWER_DRIVER*/
}

/**
 * @brief Set cellular reset pin level
 *
 * @param high 0: low level; 1: high level
 * @return
 */
RT_WEAK void cellular_hw_set_reset_pin(int high)
{
#ifdef CELLULAR_USING_MODULE_POWER_DRIVER
    if (!strlen(MODULE_RESET_PIN_NAME))
    {
        return;
    }
    if (high)
    {
        rt_pin_write(rt_pin_get(MODULE_RESET_PIN_NAME), MODULE_RESET_PIN_HIGH_LEVEL);
    }
    else
    {
        rt_pin_write(rt_pin_get(MODULE_RESET_PIN_NAME), !MODULE_RESET_PIN_HIGH_LEVEL);
    }
#endif /*CELLULAR_USING_MODULE_POWER_DRIVER*/
}

/**
 * @brief Set cellular sleep control pin level
 *
 * @param high 0: low level; 1: high level
 * @return
 */
RT_WEAK void cellular_hw_set_sleep_ctrl(int high)
{
#ifdef CELLULAR_USING_MODULE_SLEEP_CTRL_DRIVER
    if (!strlen(MODULE_SLEEP_CTRL_PIN_NAME))
    {
        return;
    }
    if (high)
    {
        rt_pin_write(rt_pin_get(MODULE_SLEEP_CTRL_PIN_NAME), MODULE_SLEEP_CTRL_PIN_HIGH_LEVEL);
    }
    else
    {
        rt_pin_write(rt_pin_get(MODULE_SLEEP_CTRL_PIN_NAME), !MODULE_SLEEP_CTRL_PIN_HIGH_LEVEL);
    }
#endif /*CELLULAR_USING_MODULE_SLEEP_CTRL_DRIVER*/
}

/**
 * @brief read cellular sleep status pin
 *
 * @return
 */
RT_WEAK int cellular_hw_get_sleep_status(void)
{
#ifdef CELLULAR_USING_MODULE_SLEEP_CTRL_DRIVER
    if (strlen(MODULE_SLEEP_STATE_PIN_NAME))
        return rt_pin_read(rt_pin_get(MODULE_SLEEP_STATE_PIN_NAME));
#endif /*CELLULAR_USING_MODULE_SLEEP_CTRL_DRIVER*/
    return 0;
}

/**
 * @brief save cellular state to non-volatile
 *
 * @param state reference to module_state_t
 * @return
 */
RT_WEAK void cellular_save_state_to_nv(module_state_t state)
{

}

/**
 * @brief read cellular state from non-volatile
 *
 * @return
 */
RT_WEAK module_state_t cellular_read_state_from_nv(void)
{
    return MODULE_OFF;
}

/**
 * @brief save used sim index to non-volatile
 *
 * @param index sim index
 * @return
 */
RT_WEAK void cellular_save_used_sim_to_nv(int index)
{

}

/**
 * @brief read used sim index from non-volatile
 *
 * @return
 */
RT_WEAK module_state_t cellular_read_used_sim_from_nv(void)
{
    return MODULE_OFF;
}

/* End of file****************************************************************/

